﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Curriculum_design
{
    public enum Way
    {
        EAST,
        SOUTH,
        WEST,
        NORTH
    }
	public partial class MainForm : Form
	{
	    Snake snake = new Snake();
		Food foods = new Food();
		private Thread game;
		private Boolean flag = false;
		//创建委托
		private delegate void DrawDele();
		private DrawDele drawDelegate;

		public MainForm()
		{
			InitializeComponent();
		}
		//点击开始游戏
		private void Button2_Click(object sender, EventArgs e)
		{
			//画蛇
			snake.DrawSnake();
			game = new Thread(new ThreadStart(StartGame));
			game.Start();

			this.Button2.Enabled = false;
			this.Focus();
		}
		/// <summary>
		/// 开始游戏
		/// </summary>
		private void StartGame()
		{
			drawDelegate = new DrawDele(PutFood);
			this.Invoke(drawDelegate, null);
			while (true)
			{
				//为当前线程指定时间
				Thread.Sleep(snake.Speed);
				if (this.IsGameOver())
				{
					MessageBox.Show("GAME OVER");
					//获取当前线程的执行状态
					if (game.IsAlive)
					{
						//终止线程
						game.Abort();
					}
				}
				if (snake.Eat(foods.FoodPoint))
				{
					snake.FoodCount++; 
					SetText(System.Convert.ToString(snake.FoodCount * 50));
					//消除原本食物
					drawDelegate = new DrawDele(KillFood);
					this.Invoke(drawDelegate, null);
					
					//重新放置食物
					drawDelegate = new DrawDele(PutFood);
					this.Invoke(drawDelegate, null);
				}
				if (textBox1.Text=="1000") {
					MessageBox.Show("成功通关！");
					//挂起线程
					game.Suspend();	
				}
				drawDelegate = new DrawDele(MoveSnake);
				this.Invoke(drawDelegate, null);
			}
		}
		//键盘控制方向
		private void mainForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyData == Keys.Left)
				this.snake.SnakeWay = (this.snake.SnakeWay == Way.EAST) ? Way.EAST : Way.WEST;
			else if (e.KeyData == Keys.Right)
				this.snake.SnakeWay = (this.snake.SnakeWay == Way.WEST) ? Way.WEST : Way.EAST;
			else if (e.KeyData == Keys.Up)
				this.snake.SnakeWay = (this.snake.SnakeWay == Way.SOUTH) ? Way.SOUTH : Way.NORTH;
			else if (e.KeyData == Keys.Down)
				this.snake.SnakeWay = (this.snake.SnakeWay == Way.NORTH) ? Way.NORTH : Way.SOUTH;
		}
		/// <summary>
		/// 放置食物
		/// </summary>
		private void PutFood()
		{
			Random rand = new Random();
			//返回一个小于指定数的随机数
			int x = rand.Next(510);
			int y = rand.Next(230);
			//食物设置
			Label food = new Label();
			food.Size = foods.Size;
			food.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			food.BackColor = foods.FoodColor;
			if (x<10) {
				x += 10;
			}
			if (y<10) {
				y += 10;
			}
			food.Location = new Point(x % 10 == 0 ? x : x + (10 - x % 10), y % 10 == 0 ? y : y + (10 - y % 10));
			foods.FoodPoint = new Point(food.Left, food.Top);
			panel1.Controls.Add(food);
		}
		
		/// <summary>
		/// 清除食物
		/// </summary>
		private void KillFood()
		{
			ClearFood(foods.FoodPoint);
		}
		private void ClearFood(Point food)
		{
			panel1.Controls.Remove(panel1.GetChildAtPoint(food));
		}

		/// <summary>
		/// 判断游戏是否结束
		/// </summary>
		/// <returns></returns>
		private bool IsGameOver()
		{
			ArrayList temp = snake.GetSnake();
			Label head = (Label)temp[0];
			foreach (Label lbl in temp.GetRange(1, temp.Count - 1))
			{
				//判断是否碰撞身体
				if (lbl.Left == head.Left && lbl.Top == head.Top)
				{
					return true;
				}
			}
			//判断是否碰撞墙壁
			if (((Label)snake.GetSnake()[0]).Left == 0 || ((Label)snake.GetSnake()[0]).Left == 520 || 
				((Label)snake.GetSnake()[0]).Top == 0 || ((Label)this.snake.GetSnake()[0]).Top == 240)
			{
				return true;
			}
			return false;
		}
		//蛇移动
		private void MoveSnake()
		{
			snake.Move(panel1);
		}
		//游戏规则
        private void operate_Click(object sender, EventArgs e)
        {
			MessageBox.Show("通过按下 上下左右控制蛇的方向，寻找吃的东西，每吃一口就能得到" +
				"一定的积分，而且蛇的身子会越吃越长，身子越长玩的难度就越大，不能碰墙，" +
				"不能咬到自己的身体，更不能咬自己的尾巴，达到了1000分，就能过关。");
        }

		private delegate void SetTextCallback(string text);
		//在给textBox1.text赋值的地方调用以下方法即可
		private void SetText(string text)
		{
			// InvokeRequired需要比较调用线程ID和创建线程ID
			// 如果它们不相同则返回true
			if (this.textBox1.InvokeRequired)
			{
				SetTextCallback d = new SetTextCallback(SetText);
				this.Invoke(d, new object[] { text });
			}
			else
			{
				this.textBox1.Text = text;
			}
		}
		//设置颜色
        private void Red_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Red;
        }
		//设置颜色
		private void Yellow_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Yellow;
		}
		//设置颜色
		private void Orange_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Orange;
		}
		//设置颜色
		private void Green_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Green;
		}
		//设置颜色
		private void Blue_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Blue;
		}
		//设置颜色
		private void White_Click(object sender, EventArgs e)
        {
			snake.Color = Color.White;
		}
		//设置颜色
		private void Purple_Click(object sender, EventArgs e)
        {
			snake.Color = Color.Purple;
		}
		//暂停游戏
		private void stop_Click(object sender, EventArgs e)
        {
			flag = true;
			game.Suspend();
		}
		//继续游戏
        private void go_Click(object sender, EventArgs e)
        {
			if (flag == true)
			{
				game.Resume();
			}
			else {
				MessageBox.Show("游戏未暂停！！！");
			}
			
        }
		//退出游戏
        private void quit_Click(object sender, EventArgs e)
        {
			game.Suspend();

			MainForm.ActiveForm.Close();        
		}
		//游戏模式
        private void Simple_Click(object sender, EventArgs e)
        {
			snake.Speed = 500;
        }
		//游戏模式
		private void ordinary_Click(object sender, EventArgs e)
        {
			snake.Speed = 100;
		}
		//游戏模式
		private void difficulty_Click(object sender, EventArgs e)
        {
			snake.Speed = 50;
		}
		//游戏模式
		private void god_Click(object sender, EventArgs e)
        {
			snake.Speed = 20;
		}
    }
}
